# -*- coding:utf-8 -*-

from . import project
from flask import render_template,abort,session
from admin_model import List_,Code,Table_show,Code_1
from project_zczh.tool.common import login_check
from project_zczh.tool.check_block_show import show_block
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import datetime
from project_zczh import redis_store
from flask_wtf.csrf import generate_csrf

@project.route('/index')
@show_block
@login_check

def index():


    table_data=list()
    info = list()
    codes=Code.query.filter_by(is_show=1).all()
    codes_1 = Code.query.filter_by(is_show=1).all()
    if len(codes) == 0 and len(codes_1) == 0:
        return '暂时没有跟新数据'
    from config import timedelta

    now = datetime.datetime.now()
    now_time =now.strftime("%Y-%m-%d")
    timedelta = datetime.timedelta(timedelta)
    check_time = (now - timedelta).strftime("%Y-%m-%d")
    code_1=Code_1.query.all()
    code_1_id_list=[code.id for code in code_1]
    for i in code_1_id_list:
        try:
            data=Code_1.query.filter_by(id=i,is_show=1).all()
        except Exception as f:
            print f
            abort(500)
        if len(data) != 0:
            data = data[0]
            code_name=data.name
            table_show_datas=Table_show.query.filter_by(table_class_id=i,mark=1).all()
            data_2={
                'name': code_name,
                'data': table_show_datas,
            }
            table_data.append(data_2)
    list_datas = List_.query.filter_by(mark=1).filter(List_.update_time > check_time).order_by(List_.update_time.desc()).all()[:3]
    if list_datas == [] :
        return '暂时没有跟新数据'
    code_id_list=[code.id for code in codes]
    for i in code_id_list:
        try:
            data=Code.query.filter_by(id=i).all()[0]
        except Exception as f:
            print f
            abort(500)
        code_name=data.name
        now = datetime.datetime.now()
        from config import timedelta
        timedelta = datetime.timedelta(timedelta)
        check_time = (now - timedelta).strftime("%Y-%m-%d")
        list_datas=List_.query.filter_by(code_id=i,mark=1).filter( List_.update_time>check_time).order_by(List_.update_time.desc()).all()[:3]
        print [data.id for data in list_datas ]
        data={
            'num':i,
            'name':code_name,
            'data':list_datas
        }
        info.append(data)
    user_id = session.get('user_id')
    titles_id = redis_store.lrange(user_id, 0, -1)
    token=generate_csrf()
    if titles_id==[]:
        return render_template('index.html',
                               info=info,
                               table_data=table_data,
                               now_time=now_time,
                               token=token,
                               code_1=code_1)
    else:
        titles_id=','.join(titles_id)
        return render_template('index.html',
                               info=info,
                               titles_id=titles_id,
                               table_data=table_data,
                               now_time=now_time,
                               token=token,
                               code_1=code_1)


